// 定时器任务
export const timeoutTask = (task, time) => {
  const tmpTimeout = setTimeout(() => {
    task();
    clearTimeout(tmpTimeout);
  }, time);
};

// 获取url中的参数
export const getUrlParam = (key) => {
  const tmpRes = window.location.search.match(new RegExp(`[?&]${key}=([^&]+)`, 'i'));
  let result;
  if (tmpRes === null || tmpRes.length < 1) {
    result = '';
  } else {
    [, result] = tmpRes;
  }
  return result;
};

// 生成随机数(min到max,不包括max)
export const generateRandom = (min, max) => {
  return Math.floor(Math.random() * (max - min)) + min;
};

// 生成随机id(随机字母加当前时间戳构成)
export const generateRandomId = (pLen) => {
  let tmpId = '';
  const charStr = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz';
  const tmpLen1 = charStr.length;
  const tmplen2 = pLen || 6;// 生成的id字母部分的长度，默认长度为6
  for (let i = 0; i < tmplen2; i += 1) {
    tmpId += charStr.charAt(Math.floor(Math.random() * tmpLen1));
  }
  tmpId += new Date().getTime();
  return tmpId;
};

const ismobile = () => {
  const u = navigator.userAgent;
  let tmpResult = '';
  if (/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))) {
    if (window.location.href.indexOf('?mobile') < 0) {
      try {
        if (/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)) {
          tmpResult = '0';
        }
        tmpResult = '1';
      } catch (e) {
        console.log(e);
      }
    }
  } else if (u.indexOf('iPad') > -1) {
    tmpResult = '0';
  } else {
    tmpResult = '1';
  }
  return tmpResult;
};
export const newDateTransform = (timedata) => {
  let target = 3;
  if (ismobile() === '0') {
    target = -5;
  }
  const date = new Date(timedata.substr(0, 19));
  const currentTime = date.getTime();
  const offset = date.getTimezoneOffset() * 60000;
  const utcTime = currentTime + offset;
  const newDate = new Date(utcTime + target * 3600000);
  const Year = newDate.getFullYear();
  const Month = newDate.getMonth() + 1 < 10 ? `0${newDate.getMonth() + 1}` : newDate.getMonth() + 1;
  const d = newDate.getDate() < 10 ? `0${newDate.getDate()}` : newDate.getDate();
  const Hours = newDate.getHours() < 10 ? `0${newDate.getHours()}` : newDate.getHours();
  const Minutes = newDate.getMinutes() < 10 ? `0${newDate.getMinutes()}` : newDate.getMinutes();
  const Seconds = newDate.getSeconds() < 10 ? `0${newDate.getSeconds()}` : newDate.getSeconds();
  const overTime = `${Year}/${Month}/${d} ${Hours}:${Minutes}:${Seconds}`;
  return overTime;
};
// 获取圆形头像
export const getRoundImg = (img) => {
  const canvas = document.createElement('canvas');
  canvas.width = img.width;
  canvas.height = img.height;
  const r = canvas.width / 2;
  const ctx = canvas.getContext('2d');
  ctx.fillStyle = ctx.createPattern(img, 'no-repeat');
  ctx.clearRect(0, 0, img.width, img.height);
  ctx.arc(img.width / 2, img.height / 2, r, 0, Math.PI * 2);
  ctx.fill();
  const dataURL = canvas.toDataURL('image/png');
  return dataURL;
};

export const loadImg = (path) => new Promise((resolve, reject) => {
  const img = new Image();
  img.crossOrigin = '';
  img.src = path;
  img.onload = () => {
    resolve(img);
  };
  img.onerror = () => {
    reject();
  };
});

// 判断字符串是否以某个字符串结尾
export const isEndWithX = (tStr, endStr) => {
  const d = tStr.length - endStr.length;
  return (d >= 0 && tStr.lastIndexOf(endStr) === d);
};
